/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import model.Activity;
import model.Guest;
import model.Signup;
import persistence.SignupDAO;

/**
 *
 * @author Martin Mohr Pedersen
 */
public class SignupController {
    
    private Guest currentGuest;
    private Activity currentActivity;
    private Signup pendingSignup;
    
    
    public int countSignups(Activity activity) {
        SignupDAO dao = new SignupDAO();
        return dao.countSignups(activity);
    }
    
    public void createSignup(Activity activity, String cpr) throws NoSuchGuestException {
        
        // find guest
        Guest guest = new GuestController().findGuest(cpr);
        
        if(guest == null) {
            throw new NoSuchGuestException();
        }
        
        pendingSignup = new Signup(activity, guest);
    }
    
    public void confirmSignup() {
        
        SignupDAO dao = new SignupDAO();
        
        dao.create(pendingSignup);
        pendingSignup = null;
    }
    
    public boolean hasFreeSlots(Activity activity) {
        if (! activity.getRequiresSignup()) {
            return true;
        }
        int max = activity.getMaxParticipants();
        int count = countSignups(activity);
        
        return max > count;
    }

    public Signup getPendingSignup() {
        return pendingSignup;
    }
    
}